/*
 * MCAL_Timer.c
 *
 *  Created on: Sep 26, 2025
 *      Author: ADMIN
 */


#include "MCAL_Timer.h"

HWTimer_ID_Type HWTimer_ID_0x06;
HWTimer_ID_Type HWTimer_ID_0x07;
HWTimer_ID_Type HWTimer_ID_0x09;

/* This Function is uesd to initialize the Timer */
Std_ReturnType MCAL_Timer_Init(void)
{
    HW_Timer_Creat(&HWTimer_ID_0x06,&htim6);
    HW_Timer_Creat(&HWTimer_ID_0x07,&htim7);
    HW_Timer_Creat(&HWTimer_ID_0x09,&htim9);
    return E_OK;
}

/* DeInit Function is uesd to Reinitialize the Timer */
Std_ReturnType MCAL_Timer_DeInit(void)
{
    HAL_TIM_Base_MspDeInit(HWTimer_ID_0x06.timerhandle);
    HAL_TIM_Base_MspDeInit(HWTimer_ID_0x07.timerhandle);
    HAL_TIM_Base_MspDeInit(HWTimer_ID_0x09.timerhandle);
    return E_OK;
}

/* This function can Start the  Timer*/
Std_ReturnType HW_Timer_Start(HWTimer_ID_Type* tim_num)
{
    HAL_TIM_Base_Start_IT(tim_num->timerhandle);
    return E_OK;
}

/* This function can Stop the  Timer*/
Std_ReturnType HW_Timer_Stop(HWTimer_ID_Type* tim_num)
{
    HAL_TIM_Base_Stop_IT(tim_num->timerhandle);
    return E_OK;
}

/* This function can Clean the Timer Cnt*/
Std_ReturnType HW_Timer_CleanCnt(HWTimer_ID_Type* tim_num)
{
    __HAL_TIM_SET_COUNTER(tim_num->timerhandle,0);
    return E_OK;
}

/* This function can Get the Timer Cnt*/
Std_ReturnType HW_Timer_GetCnt(HWTimer_ID_Type* tim_num)
{
    tim_num->now_cnt = __HAL_TIM_GET_COUNTER(tim_num->timerhandle);
    return E_OK;
}

/* This function can Clean the Timer NVIC Flag*/
Std_ReturnType HW_Timer_Cleanflag(HWTimer_ID_Type* tim_num)
{
    __HAL_TIM_CLEAR_IT(tim_num->timerhandle, TIM_IT_UPDATE);
    return E_OK;
}

/* Creat a new Timer */
void HW_Timer_Creat(HWTimer_ID_Type* tim_num,TIM_HandleTypeDef* timhandle)
{
    tim_num->timerhandle = timhandle;
    tim_num->now_cnt = 0;
    tim_num->period_cnt = timhandle->Init.Period;
    tim_num->prescale = timhandle->Init.Prescaler;
    tim_num->Start = HW_Timer_Start;
    tim_num->Stop = HW_Timer_Stop;
    tim_num->CleanCnt = HW_Timer_CleanCnt;
    tim_num->Cleanflag = HW_Timer_Cleanflag;
    tim_num->GetCnt = HW_Timer_GetCnt;
}
